var app = getApp(),
  a = app.requirejs('core');
var loading = false;

function min(a, e) {
  return a = parseFloat(a), (e = parseFloat(e)) < a ? e : a;
}

Page({
  data: {
    cash_max_day: -1,
    selected: -1
  },
  onLoad: function(a) {

  },
  onShow: function() {
    var that = this,
      cash_max_day = that.data.cash_max_day,
      cache = app.getCache('shareSetting');

    a.get('Distribe/userinfo', {
      uid: app.getCache('userinfo').uid
    }, function(t) {
      if (t.code == 0) {
        if (cache.max_money && cache.max_money != 0.00) {
          cash_max_day = parseFloat(cache.max_money) - parseFloat(t.info.today_price);
        }
        that.setData({
          user_info: t.info,
          share_setting: cache,
          cash_max_day: cash_max_day,
          show: true
        })
      } else {
        a.alert(t.msg, function() {
          a.jump('', 5)
        })
      }
    }, true)
  },
  onPullDownRefresh: function() {},
  formSubmit: function(v) {
    var e = this,
      t = parseFloat(parseFloat(v.detail.value.cash).toFixed(2)),
      i = e.data.user_info.price;
    if (-1 != e.data.cash_max_day && (i = min(i, e.data.cash_max_day)), t)
      if (i < t)
        a.alert("提现金额不能超过" + i + "元");
      else if (t < parseFloat(e.data.share_setting.min_money)) a.alert("提现金额不能低于" + e.data.share_setting.min_money + "元");
    else {
      var n = e.data.selected;
      if (0 == n || 1 == n || 2 == n || 3 == n) {
        if (0 == n || 1 == n || 2 == n) {
          if (!(c = v.detail.value.name) || null == c) return void
          a.warning('姓名不能为空');

          if (!(o = v.detail.value.mobile) || null == o) return void
          a.warning('账号不能为空');
        }
        if (2 == n) {
          if (!(s = v.detail.value.bank_name) || null == s) return void a.warning('开户行不能为空');
        } else var s = "";
        if (3 == n) {
          s = "";
          var o = "",
            c = "";
        }
        //form_id: v.detail.formId
        if (loading) {
          return;
        }
        loading = true;
        a.loading('正在提交');
        a.get('Distribe/addCash', {
          price: t,
          name: c,
          mobile: o,
          bank_name: s,
          pay_type: n,
          user_id: app.getCache('userinfo').uid
        }, function(t) {
          a.hideLoading();
          loading = false;
          if (t.code == 0) {
            a.success('申请成功');
            setTimeout(function() {
              a.jump('../index/index', 2)
            }, 1e3)
          } else {
            a.alert(t.msg)
          }
        })

      } else a.warning('请选择提现方式');
    } else a.warning('请输入提现金额');
  },
  showCashMaxDetail: function() {
    wx.showModal({
      title: "提示",
      content: "今日剩余提现金额=平台每日可提现金额-今日所有用户提现金额"
    });
  },
  select: function(a) {
    var e = a.currentTarget.dataset.index;
    e != this.data.check && this.setData({
      name: "",
      mobile: "",
      bank_name: ""
    }), this.setData({
      selected: e
    });
  }
});